#include "MessageHeader.h"
#include "BytesEndian.h"

namespace xncc {
namespace msg {
    Header MessageHeader::decode(const char* buf, uint32_t len)
    {
        Header h;
        if (len < sizeof(Header)) {
            return h;
        }
        // 解码，把头指针信息解析出来
        const auto* hPtr       = reinterpret_cast<const Header*>(buf);
        uint32_t    tmpId      = foundation::networkToHost32(hPtr->id);
        uint32_t    tmpBodyLen = foundation::networkToHost32(hPtr->bodyLen);
        h.id                   = tmpId;
        h.bodyLen              = tmpBodyLen;
        return h;
    }

    Header MessageHeader::encode(types::message_id_t id, uint32_t len)
    {
        Header h;
        h.id      = foundation::hostToNetwork32(id);
        h.bodyLen = foundation::hostToNetwork32(len);
        return h;
    }

}  // namespace msg
}  // namespace xncc